# ammo class

import common
import pygame

class Ammo:
    speed = 6
    size = [4,8]
    hit_radius = 10 # squared
    img_name = "ammo"
    x_offset = -size[0]/2
    y_offset = -size[1]/2
    def __init__(self,tower,creep):
        self.tower = tower
        self.creep = creep
        self.x = tower.x
        self.y = tower.y       
        self.base_image = common.images[self.img_name].copy()
        self.image = self.base_image
        
    def update(self):
        angle = common.get_angle(self.x-self.creep.x,self.y-self.creep.y)        
        self.image = pygame.transform.rotate(self.base_image,common.rad_deg(angle))        
        x, y = common.get_xy(angle, self.speed)
       
        self.x += x
        self.y += y

    def hit_test(self):        
        if common.get_dist_sqr(self.x - self.creep.x, self.y-self.creep.y) < self.hit_radius:
            self.creep.shot(self.tower.attack_dmg)
            if self.tower.effect <> None:
                self.creep.effect(self.tower.effect)
            return True